home *** CD-ROM | disk | FTP | other *** search
/ PCMania 64 / PCMania CD64_1.iso / phy / phy006 / lands / marsinfo.txt < prev   
Text File  |  1994-03-03  |  7KB  |  153 lines

  1.                 Voxel landscapes and How I did it
  2.                 ---------------------------------
  3.  
  4.  This document describes the method I used in my demo of a Martian terrain,
  5. which can be found at garbo.uwasa.fi:/pc/demo/mars10.zip.
  6.  It's similar to a floating horizon hidden line removal algorithm, so you'll
  7. find discussion of the salient points in many computer graphics books. The
  8. difference is the vertical line interpolation.
  9.  
  10.  
  11. First, some general points:
  12. ---------------------------
  13.  
  14.  The map is a 256x256 grid of points, each having and 8-bit integer height
  15. and a colour. The map wraps round such that, calling w(u,v) the height at
  16. (u,v), then w(0,0)=w(256,0)=w(0,256)=w(256,256). w(1,1)=w(257,257), etc.
  17.  
  18.  Map co-ords: (u,v) co-ordinates that describe a position on the map. The
  19. map can be thought of as a height function w=f(u,v) sampled discretely.
  20.  
  21.  Screen co-ords: (x,y) co-ordinates for a pixel on the screen.
  22.  
  23.  
  24. To generate the map:
  25. --------------------
  26.  
  27.  This is a recursive subdivision, or plasma, fractal. You start of with
  28. a random height at (0,0) and therefore also at (256,0), (0,256), (256,256).
  29. Call a routine that takes as input the size and position of a square, in the
  30. first case the entire map.
  31.  This routine get the heights from the corners of the square it gets given.
  32. Across each edge (if the map has not been written to at the point halfway
  33. along that edge), it takes the average of the heights of the 2 corners on that
  34. edge, applies some noise proportional to the length of the edge, and writes
  35. the result into the map at a position halfway along the edge. The centre of
  36. the square is the average of the four corners+noise.
  37.  The routine then calls itself recursively, splitting each square into four
  38. quadrants, calling itself for each quadrant until the length of the side is
  39. 2 pixels.
  40.  This is probably old-hat to many people, but the map is made more realistic
  41. by blurring:
  42.  
  43.      w(u,v)=k1*w(u,v)+k2*w(u+3,v-2)+k3*w(u-2,v+4) or something.
  44.  
  45.  Choose k1,k2,k3 such that k1+k2+k3=1. The points at which the map is sampled
  46. for the blurring filter do not really matter - they give different effects,
  47. and you don't need any theoretical reason to choose one lot as long as it
  48. looks good. Of course do everything in fixed point integer arithmetic.
  49.  The colours are done so that the sun is on the horizon to the East:
  50.  
  51.      Colour=A*Ä w(u+1,v)-w(u,v) Å+B
  52.  
  53. with A and B chosen so that the full range of the palette is used.
  54.  The sky is a similar fractal but without the colour transformation.
  55.  
  56.  
  57. How to draw each frame
  58. ----------------------
  59.  
  60.  First, draw the sky, and blank off about 50 or so scan lines below the
  61. horizon since the routine may not write to all of them (eg. if you are on top
  62. of a high mountain looking onto a flat plane, the plane will not go to the
  63. horizon).
  64.  Now, down to business. The screen is as follows:
  65.  
  66.      ---------------------------
  67.      ö                         ö
  68.      ö                         ö
  69.      ö           Sky           ö
  70.      ö                         ö
  71.      ö                         ö
  72.      öa------------------------ö Horizon
  73.      ö                         ö
  74.      ö                         ö    Point (a)=screen co-ords (0,0)
  75.      ö          Ground         ö     x increases horizontally
  76.      ö                         ö     y increases downwards
  77.      ö                         ö
  78.      ---------------------------
  79.  
  80.  Imagine the viewpoint is at a position (p,q,r) where (p,q) are the (u,v)
  81. map co-ordinates and r is the altitude. Now, for each horizontal (constant v)
  82. line of map from v=r+100 (say) down to v=r, do this:
  83.  
  84.   1. Calculate the y co-ordinate of map co-ord (p,v,0) (perspective transform)
  85.  
  86.   2. Calculate scale factor f which is how many screen pixels high a mountain
  87. of constant height would be if at distance v from q. Therefore, f is small
  88. for map co-ords far away (v>>r) and gets bigger as v comes down towards r.
  89.  
  90.   3. Work out the map u co-ord corresponding to (0,y). v is constant along
  91. each line.
  92.  
  93.   4. Starting at the calculated (u,v), traverse the screen, incrementing the
  94. x co-ordinate and adding on a constant, c, to u such that (u+c,v) are the map
  95. co-ords corresponding to the screen co-ords (1,y). You then have 256 map
  96. co-ords along a line of constant v. Get the height, w, at each map co-ord and
  97. draw a spot at (x,y-w*f) for all x.
  98.  
  99.  Sorry, but that probably doesn't make much sense. Here's an example:
  100. Imagine sometime in the middle of drawing the frame, everything behind a
  101. point (say v=q+50) will have been drawn:
  102.  
  103.      ---------------------------
  104.      ö                         ö
  105.      ö                         ö
  106.      ö                         ö
  107.      ö           ****          ö
  108.      ö        *********        ö <- A mountain half-drawn.
  109.      ö-----**************------ö
  110.      ö*************************ö
  111.      ö*********       *********ö
  112.      ö******             ******ö
  113.      ö.........................ö <- The row of dots is at screen co-ord y
  114.      ö                         ö   corresponding to an altitude of 0 for that
  115.      ---------------------------   particular distance v.
  116.  
  117.  Now the screen-scanning routine will get called for v=q+50. It draws in a
  118. point for every x corresponding to heights at map positions (u,v) where u
  119. goes from p-something to p+something, v constant. The routine would put points
  120. at these positions: (ignoring what was there before)
  121.  
  122.      ---------------------------
  123.      ö                         ö
  124.      ö                         ö
  125.      ö                         ö
  126.      ö                         ö
  127.      ö                         ö
  128.      ö-------------------------ö
  129.      ö          *****          ö
  130.      ö       ***     ***       ö
  131.      ö*******           *******ö
  132.      ö.........................ö
  133.      ö                         ö
  134.      ---------------------------
  135.  
  136.  So, you can see that the screen gets drawn from the back, one vertical
  137. section after another. In fact, there's more to it than drawing one pixel
  138. at every x during the scan - you need to draw a vertical line between
  139. (x,y old) to (x,y new), so you have to have a buffer containing the y values
  140. for every x that were calculated in the previous pass. You interpolate
  141. along this line (Gouraud style) from the old colour to the new colour also,
  142. so you have to keep a buffer of the colours done in the last pass.
  143.  Only draw the vertical lines if they are visible (ie. going down,
  144. y new>y old). The screen is drawn from the back so that objects can be drawn
  145. inbetween drawing each vertical section at the appropriate time.
  146.  
  147.  If you need further information or details, mail me or post here... Posting
  148. will allow others to benefit from your points and my replies, though.
  149.  
  150.  Thank you for the response I have received since uploading this program.
  151.  
  152.  Tim Clarke, tjc1005@hermes.cam.ac.uk      
  153.